package com.training.bt.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import jakarta.persistence.*;

@Data
@TableName("tb_promotion_resource")
public class PromotionResource {
    @TableId(type = IdType.AUTO)
    private Long relationId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "promotion_id", nullable = false)
    @TableField(exist = false) // 标记为非数据库字段
    private Promotion promotion;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "resource_id", nullable = false)
    @TableField(exist = false) // 标记为非数据库字段
    private Resource resource;

    private Long promotionId;
    private Long resourceId;

    @PrePersist
    @PreUpdate
    public void updateIds() {
        if (this.promotion != null) {
            this.promotionId = this.promotion.getPromotionId();
        }
        if (this.resource != null) {
            this.resourceId = this.resource.getId();
        }
    }
}